package com.per.note.ui.countadd;

import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.per.note.R;
import com.per.note.bean.User;
import com.per.note.db.SqliteManage;
import com.per.note.ui.input.InputActivity;
import com.per.note.ui.load.MyApplication;

/**
 * 添加账户操作
 */
public class AddCountActivity extends AppCompatActivity {
    private EditText mEt;
    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_count);
        initView();
    }

    /**获取view的id*/
    private void initView() {
        mEt = (EditText) findViewById(R.id.countadd_et_name);
        mTv = (TextView) findViewById(R.id.countadd_tv_count);
    }

    /**定义点击事件*/
    public void onClick(View v) {
        if (v == null) return;      //view为空则返回
        switch (v.getId()) {
            case R.id.countadd_ib_return:
                finish();
                break;
            case R.id.countadd_ll_count:     //跳转到数字输入页面
                Intent intent = new Intent(this, InputActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.countadd_ib_add:   // 当金额不为零且账户长度大于0时确认添加账户
                if (mEt.getText().toString().length() > 0 && !mTv.getText().toString().equals("00.00")) {
                    addCount();
                } else if (mTv.getText().toString().equals("00.00")) {
                    Toast.makeText(AddCountActivity.this, "金额不能为0", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Toast.makeText(AddCountActivity.this, "账户长度不能小于1", Toast.LENGTH_SHORT).show();
                    return;
                }
                finish();
                break;
            default:
                break;
        }
    }

    /**添加一个新的账户信息*/
    private void addCount() {
        if (SqliteManage.getInstance(this).isExistInTable("count", "count=? and userid=?", new String[]{mEt.getText().toString(),String.valueOf(MyApplication.userid)})) {
            Toast.makeText(this, "该账户已存在", Toast.LENGTH_SHORT).show();
        } else {
            ContentValues values = new ContentValues();
            values.put("userid",String.valueOf(MyApplication.userid));
            values.put("count", mEt.getText().toString());
            values.put("money", mTv.getText().toString());
            SqliteManage.getInstance(this).insertItem("count", values);
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == 1) {
            mTv.setText(data.getStringExtra("msgmoney"));
        }
    }
}
